package com.info33.platform.business.app.controller;

import com.info33.platform.business.app.service.AppLoginService;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.RedisGeo;
import com.info33.platform.system.dto.SysUserDTO;
import com.info33.platform.system.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author debj
 * @version 1.0
 * @date 2021/8/9 16:05
 */
@RestController
@RequestMapping("/AppLogin")
@Api(tags = "app - 登录")
public class AppLoginController {

    @Autowired
    private AppLoginService appLoginService;
    @Autowired
    private RedisGeo redisGeo;

    @PostMapping("/password/login")
    @ApiOperation(value = "app - 账号密码登录")
    public Result<Object> passwordLogin(
            @Validated(value = SysUserDTO.AppPassWordLogin.class) @RequestBody SysUserDTO dto) {
        return appLoginService.passwordLogin(dto);
    }

    @PostMapping(value = "/message/push/login")
    @ApiOperation(value = "app - 发送验证码", notes = "登录时 + 忘记密码")
    public Result<Object> messagePushByLogin(
            @Validated(value = SysUserDTO.MessagePush.class) @RequestBody SysUserDTO dto) {
        return appLoginService.messagePushByLogin(dto);
    }

    @PostMapping("/message/login")
    @ApiOperation(value = "app - 手机号验证码登录")
    public Result<Object> messageLogin(
            @Validated(value = SysUserDTO.MessageLogin.class) @RequestBody SysUserDTO dto) {
        return appLoginService.messageLogin(dto);
    }

    @PostMapping(value = "/message/push/register")
    @ApiOperation(value = "app - 发送验证码", notes = "注册时")
    public Result<Object> messagePushByRegister(
            @Validated(value = SysUserDTO.MessagePush.class) @RequestBody SysUserDTO dto) {
        return appLoginService.messagePushByRegister(dto);
    }

    @PostMapping(value = "/app/register")
    @ApiOperation(value = "app - 注册")
    public Result<Object> appRegister(
            @RequestBody SysUserDTO dto) {
        return appLoginService.appRegister(dto);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "登出")
    public Result<Object> logout() {
        Subject subject = SecurityUtils.getSubject();
        SysUserVO appUserVO = (SysUserVO) subject.getPrincipal();
        // 清除用户经纬度信息
        redisGeo.remove(appUserVO.getAppTenantUserVO().getId() + "");
        subject.logout();
        return Result.ok();
    }

}
